@@ -14,7 +14,7 @@ from message.models import UserMessageInfo |
||
| 14 | 14 |
from group.serializers import GroupInfoSerializer, GroupUserInfoSerializer, GroupPhotoInfoSerializer |
| 15 | 15 |
|
| 16 | 16 |
from utils.page_utils import pagination |
| 17 |
-from utils.thumbnail_utils import make_thumb |
|
| 17 |
+from utils.thumbnail_utils import make_thumbnail |
|
| 18 | 18 |
from utils.url_utils import img_url |
| 19 | 19 |
|
| 20 | 20 |
from utils.error.errno_utils import UserStatusCode, GroupStatusCode, GroupUserStatusCode, GroupPhotoStatusCode |
@@ -540,21 +540,20 @@ def flyimg_upload_api(request): |
||
| 540 | 540 |
return response(GroupUserStatusCode.GROUP_USER_NOT_FOUND) |
| 541 | 541 |
|
| 542 | 542 |
if photo: |
| 543 |
- _, extension = os.path.splitext(photo.name) |
|
| 544 |
- |
|
| 545 |
- uuid = shortuuid.uuid() |
|
| 546 |
- photo_path = 'fly/{uuid}{extension}'.format(uuid=uuid, extension=extension)
|
|
| 547 |
- photo_thumbnail_path = 'fly/{uuid}_thumbnail{extension}'.format(uuid=uuid, extension=extension)
|
|
| 543 |
+ photo_path = 'fly/{uuid}{extension}'.format(uuid=shortuuid.uuid(), extension=os.path.splitext(photo.name)[1])
|
|
| 544 |
+ photo_thumbnail_path = photo_path.replace('.', '_thumbnail.')
|
|
| 548 | 545 |
|
| 549 | 546 |
if default_storage.exists(photo_path): |
| 550 | 547 |
default_storage.delete(photo_path) |
| 551 | 548 |
default_storage.save(photo_path, photo) |
| 552 | 549 |
|
| 553 |
- if default_storage.exists(photo_thumbnail_path): |
|
| 554 |
- default_storage.delete(photo_thumbnail_path) |
|
| 555 |
- default_storage.save(photo_thumbnail_path, photo) |
|
| 550 |
+ # if default_storage.exists(photo_thumbnail_path): |
|
| 551 |
+ # default_storage.delete(photo_thumbnail_path) |
|
| 552 |
+ # default_storage.save(photo_thumbnail_path, photo) |
|
| 556 | 553 |
|
| 557 |
- photo_w, photo_h, photo_thumbnail_w, photo_thumbnail_h = make_thumb( |
|
| 554 |
+ # 群组照片缩略图生成 |
|
| 555 |
+ photo_w, photo_h, photo_thumbnail_w, photo_thumbnail_h = make_thumbnail( |
|
| 556 |
+ os.path.join(settings.MEDIA_ROOT, photo_path).replace('\\', '/'),
|
|
| 558 | 557 |
os.path.join(settings.MEDIA_ROOT, photo_thumbnail_path).replace('\\', '/'),
|
| 559 | 558 |
settings.THUMBNAIL_MAX_WIDTH |
| 560 | 559 |
) |
@@ -581,8 +580,10 @@ def flyimg_upload_api(request): |
||
| 581 | 580 |
group_id=group_id, |
| 582 | 581 |
status=True, |
| 583 | 582 |
pk__gt=max(current_id, group_user.current_id), |
| 583 |
+ ).order_by( |
|
| 584 |
+ '-pk' |
|
| 584 | 585 |
) |
| 585 |
- latest_photo = group_photos.last() |
|
| 586 |
+ latest_photo = group_photos.first() |
|
| 586 | 587 |
|
| 587 | 588 |
return JsonResponse({
|
| 588 | 589 |
'status': 200, |
@@ -4,7 +4,7 @@ from django.conf import settings |
||
| 4 | 4 |
from django.core.files.storage import default_storage |
| 5 | 5 |
from django.db import transaction |
| 6 | 6 |
from django.http import JsonResponse |
| 7 |
-from django.shortcuts import render, redirect |
|
| 7 |
+from django.shortcuts import render |
|
| 8 | 8 |
|
| 9 | 9 |
from rest_framework import viewsets |
| 10 | 10 |
|
@@ -14,13 +14,14 @@ from photo.models import UUIDInfo, PhotosInfo |
||
| 14 | 14 |
from photo.serializers import PhotosInfoSerializer |
| 15 | 15 |
|
| 16 | 16 |
from utils.ip_utils import ip_addr |
| 17 |
+from utils.thumbnail_utils import make_thumbnail |
|
| 17 | 18 |
from utils.watermark_utils import watermark_wrap |
| 18 | 19 |
|
| 19 | 20 |
from utils.error.errno_utils import PhotoStatusCode |
| 20 | 21 |
from utils.error.response_utils import response |
| 21 | 22 |
|
| 22 | 23 |
from utils.redis.rkeys import GROUP_LAST_PHOTO_PK |
| 23 |
-from utils.redis.rgroup import get_group_info, get_group_users_info |
|
| 24 |
+from utils.redis.rgroup import set_group_info, set_group_users_info |
|
| 24 | 25 |
|
| 25 | 26 |
from curtail_uuid import CurtailUUID |
| 26 | 27 |
|
@@ -149,9 +150,10 @@ def session_detail_api(request, session): |
||
| 149 | 150 |
:return: |
| 150 | 151 |
""" |
| 151 | 152 |
user_id = request.POST.get('user_id', '')
|
| 153 |
+ current_id = -1 |
|
| 152 | 154 |
|
| 153 | 155 |
# 判断 user_id 是否存在,如果不存在,则直接分配帐户 |
| 154 |
- user, created = UserInfo.objects.get_or_create(user_id=user_id, defaults={
|
|
| 156 |
+ user, user_created = UserInfo.objects.get_or_create(user_id=user_id, defaults={
|
|
| 155 | 157 |
'user_id': CurtailUUID.uuid(UserInfo, 'user_id'), |
| 156 | 158 |
'user_status': UserInfo.ASSIGN, |
| 157 | 159 |
'assign_ip': ip_addr(request), |
@@ -160,34 +162,91 @@ def session_detail_api(request, session): |
||
| 160 | 162 |
user_id = user.user_id |
| 161 | 163 |
|
| 162 | 164 |
# 判断通过 session_id 创建的群组是否存在,如果不存在,则直接创建 |
| 163 |
- group, created = GroupInfo.objects.get_or_create(session_id=session, defaults={
|
|
| 165 |
+ group, group_created = GroupInfo.objects.get_or_create(session_id=session, defaults={
|
|
| 164 | 166 |
'group_id': CurtailUUID.uuid(GroupInfo, 'group_id'), |
| 165 | 167 |
'admin_id': user_id, |
| 168 |
+ 'group_name': user.final_nickname, |
|
| 169 |
+ 'group_default_avatar': 0, |
|
| 166 | 170 |
'group_from': GroupInfo.SESSION_GROUP, |
| 167 | 171 |
'session_id': session, |
| 168 | 172 |
}) |
| 169 | 173 |
group_id = group.group_id |
| 170 | 174 |
|
| 175 |
+ # Redis 群组数据缓存 |
|
| 176 |
+ if group_created: |
|
| 177 |
+ group_info = set_group_info(group) |
|
| 178 |
+ |
|
| 171 | 179 |
# 判断 group_id/user_id 的群组用户是否存在,如果不存在,则直接创建 |
| 172 |
- group_user = GroupUserInfo.objects.get_or_create(group_id=group_id, user_id=user_id, defaults={
|
|
| 173 |
- 'current_id': int(r.get(GROUP_LAST_PHOTO_PK % group_id) or -1), |
|
| 180 |
+ group_user, group_user_created = GroupUserInfo.objects.get_or_create(group_id=group_id, user_id=user_id, defaults={
|
|
| 181 |
+ # 'current_id': int(r.get(GROUP_LAST_PHOTO_PK % group_id) or -1), |
|
| 182 |
+ 'current_id': current_id, # 通过扫描 session_id 二维码进群的用户,默认可以查看该群组所有照片 |
|
| 174 | 183 |
'nickname': user.final_nickname, |
| 175 |
- 'admin': False, |
|
| 184 |
+ 'avatar': user.avatar, |
|
| 185 |
+ 'admin': group_created, |
|
| 176 | 186 |
'user_status': GroupUserInfo.PASSED, |
| 177 | 187 |
'passed_at': tc.utc_datetime(), |
| 178 | 188 |
}) |
| 179 | 189 |
|
| 190 |
+ # Redis 群组用户数据缓存 |
|
| 191 |
+ if group_user_created: |
|
| 192 |
+ group_users = set_group_users_info(group) |
|
| 193 |
+ |
|
| 194 |
+ # 获取 Session 照片 |
|
| 180 | 195 |
photos = PhotosInfo.objects.filter(session_id=session) |
| 196 |
+ |
|
| 197 |
+ # |
|
| 198 |
+ if group_created: |
|
| 199 |
+ for photo in photos: |
|
| 200 |
+ photo_path = photo.p_photo_path |
|
| 201 |
+ photo_thumbnail_path = photo_path.replace('.', '_thumbnail.')
|
|
| 202 |
+ |
|
| 203 |
+ # 群组照片缩略图生成 |
|
| 204 |
+ photo_w, photo_h, photo_thumbnail_w, photo_thumbnail_h = make_thumbnail( |
|
| 205 |
+ os.path.join(settings.MEDIA_ROOT, photo_path).replace('\\', '/'),
|
|
| 206 |
+ os.path.join(settings.MEDIA_ROOT, photo_thumbnail_path).replace('\\', '/'),
|
|
| 207 |
+ settings.THUMBNAIL_MAX_WIDTH |
|
| 208 |
+ ) |
|
| 209 |
+ |
|
| 210 |
+ # 群组照片记录创建 |
|
| 211 |
+ group_photo = GroupPhotoInfo.objects.create( |
|
| 212 |
+ group_id=group_id, |
|
| 213 |
+ user_id=user_id, |
|
| 214 |
+ nickname=user.final_nickname, |
|
| 215 |
+ avatar=user.avatar, |
|
| 216 |
+ photo_path=photo_path, |
|
| 217 |
+ photo_w=photo_w, |
|
| 218 |
+ photo_h=photo_h, |
|
| 219 |
+ photo_thumbnail_path=photo_thumbnail_path, |
|
| 220 |
+ photo_thumbnail_w=photo_thumbnail_w, |
|
| 221 |
+ photo_thumbnail_h=photo_thumbnail_h, |
|
| 222 |
+ photo_from=GroupPhotoInfo.SESSION_GROUP, |
|
| 223 |
+ ) |
|
| 224 |
+ |
|
| 225 |
+ # 设置群组最后一张照片PK |
|
| 226 |
+ r.set(GROUP_LAST_PHOTO_PK % group_id, group_photo.pk) |
|
| 227 |
+ |
|
| 228 |
+ # 获取从 current_id 到 now 的群组照片列表 |
|
| 229 |
+ group_photos = GroupPhotoInfo.objects.filter( |
|
| 230 |
+ group_id=group_id, |
|
| 231 |
+ status=True, |
|
| 232 |
+ pk__gt=group_user.current_id, |
|
| 233 |
+ ).order_by( |
|
| 234 |
+ '-pk' |
|
| 235 |
+ ) |
|
| 236 |
+ latest_photo = group_photos.first() |
|
| 237 |
+ |
|
| 181 | 238 |
return JsonResponse({
|
| 182 | 239 |
'status': 200, |
| 183 | 240 |
'message': u'获取照片列表成功', |
| 184 | 241 |
'data': {
|
| 185 |
- 'count': photos.count(), |
|
| 186 |
- 'photos': [p.detail for p in photos], |
|
| 242 |
+ # 'count': photos.count(), |
|
| 243 |
+ # 'photos': [p.detail for p in photos], |
|
| 244 |
+ 'current_id': latest_photo and latest_photo.pk or current_id, |
|
| 245 |
+ 'photos': [photo.photo_info for photo in group_photos], |
|
| 187 | 246 |
'group_id': group_id, |
| 188 |
- 'group': get_group_info(group_id), |
|
| 247 |
+ 'group': group_info, |
|
| 189 | 248 |
'user_id': user_id, |
| 190 |
- 'users': get_group_users_info(group_id, user_id), |
|
| 249 |
+ 'users': group_users, |
|
| 191 | 250 |
} |
| 192 | 251 |
}) |
| 193 | 252 |
|
@@ -8,11 +8,11 @@ except ImportError: |
||
| 8 | 8 |
import Image |
| 9 | 9 |
|
| 10 | 10 |
|
| 11 |
-def make_thumb(im_path, max_width=360): |
|
| 11 |
+def make_thumbnail(im_path, im_thumbnail_path=None, max_width=360): |
|
| 12 | 12 |
im = Image.open(im_path) |
| 13 | 13 |
width, height = im.size |
| 14 | 14 |
thumb_width = min(max_width, width) |
| 15 | 15 |
thumb_height = height / width * thumb_width |
| 16 | 16 |
im.thumbnail((thumb_width, thumb_height)) |
| 17 |
- im.save(im_path, im.format or 'JPEG') |
|
| 17 |
+ im.save(im_thumbnail_path or im_path, im.format or 'JPEG') |
|
| 18 | 18 |
return width, height, thumb_width, thumb_height |